<?php
/**
__ROOT__<br>
__STATIC__<br>
__JS__<br>
__CSS__<br>
**/

namespace app\index\controller;

use think\Db;
use think\Request;
use think\Session;

use think\Validate;

class Index extends \think\Controller
{
	function __construct()
	{
		parent::__construct();
		$r = Request::instance();
		//echo $r->controller();		exit;
		$request = Request::instance();
		if($request->action() != 'login' && $request->action()!= 'dologin')
		{
			$this->checkAuth();
		}
	}
	#验证登陆
	function checkAuth()
	{
		
		if(!session::has('user'))
		{
			echo "没有登陆";
			echo $url = url("/index/login");
			$this->success("请先登陆", "index/login");
			exit;
		}
	}
	function login()
	{
		$rt = ['id' => 1, 'login' => 'admin'];
		session::set("user", $rt);
		return $this->fetch('login');
	}
	public function dologin()
	{
		
	}
	function right()
	{
		return $this->fetch();
	}
    public function index()
    {
		return $this->fetch();
		#添加操作
		$rt = [
			'title' => '是我',
			'remark' => '备注',
			'typename' => '早餐',
			'user_id' => 1,
			'money' => 1.6,
			'addtime' => date('Y-m-d H:i:s'),
		];
		//db()->name('in')->insert($rt);
		//$lastid = db()->getLastInsId();
		
		/*
		助手函数
		// 添加单条数据
		db('user')->insert($data);
		// 添加多条数据
		db('user')->insertAll($list);
		*/
		
		
		#编辑操作 update 方法返回影响数据的条数，没修改任何数据返回 0
		
		db()->name('in')->where('id',1)->update($rt);
		
		#更新某个字段的值：
		#Db::table('think_user')->where('id',1)->setField('name', 'thinkphp');
		#Db::name('user')->where('id',1)->setField('name', 'thinkphp');
		
		/**
		自增或自减一个字段的值

		setInc/setDec 如不加第二个参数，默认值为1

		// score 字段加 1
		Db::table('think_user')->where('id', 1)->setInc('score');
		// score 字段加 5
		Db::table('think_user')->where('id', 1)->setInc('score', 5);
		// score 字段减 1
		Db::table('think_user')->where('id', 1)->setDec('score');
		// score 字段减 5
		Db::table('think_user')->where('id', 1)->setDec('score', 5);
		*/
		
		
		/**
		// 根据主键删除
		Db::table('think_user')->delete(1);
		Db::table('think_user')->delete([1,2,3]);

		// 条件删除    
		Db::table('think_user')->where('id',1)->delete();
		Db::table('think_user')->where('id','<',10)->delete();
		*/
		
		db('in')->delete([9]);
		
		#模型
		$db = model('in');
		$db->title = "333";
		$db->remark = "备注". date('Ymd H:i:s');;
		//$db->save();
		
		
		
		
		$rt['title'] = "555";
		$rt['remark'] = "备注44" . date('Ymd H:i:s');
		$db->save($rt, ['id'=>36]);
		
		$db->where("id=44")->delete();
		$db->where(['id'=>43])->delete();
		
		$rt = $db->select("4");
		print_r($rt);
		
		//获取自增ID
		//echo $db->id;
		
		
		
		#查询操作
		//return view();
		//$build = include APP_PATH . 'build.php'; //		\think\Build::run($build);
		//$rs = db::table("live_in")->finds();
		$rs = db::name("in")->find();
		$rs = db::name("in")
		->order("id DESC")
		->select();
		//$rs = db()->name("in")->find();
		
		echo "当前共有记录:";
		echo db::name("in")->count();
		
		$this->assign('a', 'HELLO');
		
		
		
		dump($rs);
		 // 渲染模板输出
		//return view('index',['a' => 'HELLO']);
		return $this->fetch('index');
    }
	function x()
	{
		//return view();
		return __FUNCTION__;
	}
	function hell()
	{
		//return view();
		return __FUNCTION__;
	}
}
